package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.bean.ProduceOrderParentBean;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class ProductionTaskOrderListAdapter extends BaseAdapter {

    private Context context;
    private List<ProduceProduceOrderBean> datas = new ArrayList<ProduceProduceOrderBean>();
    private LayoutInflater inflater;
    private ItemClickListener clickListener;


    public ProductionTaskOrderListAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);

    }

    public void setData(List<ProduceProduceOrderBean> list) {
        if (null != list) {
            this.datas.clear();
            this.datas.addAll(list);
        }
    }

    public void setItemClick(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public ProduceProduceOrderBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_product_task_order_list, null);
            holder.tv_type = convertView.findViewById(R.id.tv_type);
            holder.tv_level = convertView.findViewById(R.id.tv_level);
            holder.tv_status = convertView.findViewById(R.id.tv_status);
            holder.tv_project_name = convertView.findViewById(R.id.tv_project_name);
            holder.tv_engineering_name = convertView.findViewById(R.id.tv_engineering_name);
            holder.tv_ishurry = convertView.findViewById(R.id.tv_ishurry);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ProduceOrderParentBean bean = datas.get(position);
        if (bean.getType().equals("标准工单")) {
            holder.tv_type.setText("A类");
        } else if (bean.getType().equals("返工工单")) {
            holder.tv_type.setText("B类");
        } else if (bean.getType().equals("报废工单")) {
            holder.tv_type.setText("C类");
        } else if (bean.getType().equals("打样工单")) {
            holder.tv_type.setText("D类");
        } else if (bean.getType().equals("免费工单")) {
            holder.tv_type.setText("E类");
        }


        CommonEnum.ProduceOrderLevel level = bean.getLevel();
        if (null != level) {
            if (bean.getLevel() == CommonEnum.ProduceOrderLevel.VERY_IMPORTANT) {
                holder.tv_level.setText("一级");
            } else if (bean.getLevel() == CommonEnum.ProduceOrderLevel.MORE_IMPORTANT) {
                holder.tv_level.setText("二级");
            } else if (bean.getLevel() == CommonEnum.ProduceOrderLevel.GENERALLY_IMPORTANT) {
                holder.tv_level.setText("三级");
            } else if (bean.getLevel() == CommonEnum.ProduceOrderLevel.NORMAL) {
                holder.tv_level.setText("四级");
            }
        }

        CommonEnum.ProduceOrderIsHurry isHurry = bean.getIsHurry();
        if (null != isHurry) {
            if (bean.getIsHurry() == CommonEnum.ProduceOrderIsHurry.HURRY) {
                holder.tv_ishurry.setVisibility(View.VISIBLE);
                holder.tv_ishurry.setText(" / 加急");
            } else if (bean.getIsHurry() == CommonEnum.ProduceOrderIsHurry.NOT_HURRY) {
                holder.tv_ishurry.setVisibility(View.GONE);
            }
        }

        CommonEnum.ProduceOrderStatus status = bean.getStatus();
        if (null != status) {
            if (status == CommonEnum.ProduceOrderStatus.CREATED) {
                holder.tv_status.setText("");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_00000000));
            } else if (status == CommonEnum.ProduceOrderStatus.SEND) {
                holder.tv_status.setText("[等待接收]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
            } else if (status == CommonEnum.ProduceOrderStatus.RETURNED) {
                holder.tv_status.setText("[退回]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_b10e1a));
            } else if (status == CommonEnum.ProduceOrderStatus.RECEIVED) {
                holder.tv_status.setText("[已经接收]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_87c688));
            } else if (status == CommonEnum.ProduceOrderStatus.COMPLETED) {
                holder.tv_status.setText("[完成]");
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_2c9a2d));
            }
        }
        holder.tv_project_name.setText(bean.getProjectName());
        holder.tv_engineering_name.setText(" / " + bean.getEngineeringName());

        return convertView;
    }

    static class ViewHolder {
        TextView tv_type;
        TextView tv_status;
        TextView tv_level;
        TextView tv_project_name;
        TextView tv_engineering_name;
        TextView tv_ishurry;

    }
}
